feat: context optimization — AST skeleton caching for Read tool token reduction#18
Merged
Merged
Conversation
New src/ast-utils.ts with findImports, findFunctions, findClasses, findExports, and extractJsPackageName. Refactored 4 detectors (god-component, god-function, mixed-concerns, undeclared-import) to use shared utils. Removed 68 lines of duplicated helpers from god-function.ts. All 568 existing detector tests pass unchanged.
Lane A: AST skeleton extraction (src/context/skeleton.ts) using shared ast-utils. Compresses 600-line files to ~50-line structural summaries. Lane B: bun:sqlite cache layer (src/context/cache.ts) with WAL mode. Tracks skeletons, session reads, and token savings stats. Benchmark command (src/context/benchmark.ts) runs under node with zero bun:sqlite dependency — projects savings without requiring bun. 38 new tests across skeleton, cache, and integration suites.
src/context.ts: PreToolUse/PostToolUse/PostCompact hook entry point. Smart-limits re-reads to 30 lines + skeleton via additionalContext. src/init.ts: --context flag generates Claude Code hooks with conflict detection. Resolves absolute paths to dist/context.js for local dev and npm installs. src/mcp/server.ts: vibecop_context_benchmark MCP tool — runs under node, no bun required. Projects token savings for any project. src/cli.ts: vibecop context [benchmark|stats] command. Benchmark runs in-process under node. Stats shells out to bun for SQLite.
scripts/validate-context.ts: 22-check validation suite (skeleton quality, latency benchmarks, end-to-end hook protocol). scripts/baseline-read-counter.ts: baseline Read counter for A/B testing. scripts/test-hook-dedup.ts: empirical hook/dedup test harness.
Version 0.4.0 → 0.4.1. CHANGELOG with full v0.4.1 entry. README: context optimization section, MCP tool count 3→4, Phase 4 done. CONTRIBUTING: updated project structure, build:context in PR checklist. Starlight docs: architecture, MCP server, Claude Code, CLI reference. .gitignore: added .vibecop/ for SQLite cache. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
v0.4.1 — Context Optimization. Intercepts Claude Code Read tool re-reads to reduce token consumption via AST skeleton caching.
src/ast-utils.tswith sharedfindImports(),findFunctions(),findClasses(),findExports(). 4 detectors refactored, 68 lines of duplication removed.bun:sqlitewith WAL mode. Tracks skeletons, session reads, and token savings per session.vibecop init --context— generates Claude Code hooks with conflict detection. Resolves absolute paths for both local dev and npm installs.vibecop context benchmark— scans project, shows per-file compression ratios and projected savings. Runs under node (no bun required).vibecop_context_benchmark— same benchmark accessible through MCP server.Test Coverage
610 tests pass (42 new). Type check clean. Lint clean. Both builds succeed.
Validation Results
22/22 validation checks pass (
bun scripts/validate-context.ts):Benchmark (on vibecop itself)
Architecture
dist/cli.js(1.32MB)dist/context.js(26KB)Test plan
🤖 Generated with Claude Code